home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / PACKET / TFLINK10 / TFLNKSRC / GETOPTS.C next >
C/C++ Source or Header  |  1996-04-14  |  5KB  |  161 lines

  1. /*
  2.  *-----------------------------------------------------------------------
  3.  *
  4.  * TFLINK - program to make TFPCX TSR running a Baycom modem on a
  5.  * COM port appear as a serial (HOST mode) TNC on a second COM port.
  6.  *
  7.  * getopts.c - handle command line options
  8.  *
  9.  *-----------------------------------------------------------------------
  10.  */
  11.  
  12. /* standard include file */
  13. #include "tflink.h"
  14.  
  15. /* local definitions */
  16. /* usage and help messages */
  17. #define USAGE    "\
  18. Usage: TFLINK -P COMn [ -N ] [ -B baud ] [ -I interrupt ]\n\
  19.        TFLINK -P COMn [ -N ] -U\n\n\
  20.        TFLINK -H for help\n"
  21. #define HELPMSG    "\
  22. \n\
  23. Usage: TFLINK -P COMn [ -N ] [ -B baud ] [ -I interrupt ]\n\
  24.        TFLINK -P COMn [ -N ] -U\n\
  25. \n\
  26.   -P COM<n>       Link TFPCX to port COM<n>.  COM1 to COM4 allowed.\n\
  27.                   No default value.\n\
  28. \n\
  29.   -N              No startup or unload messages\n\
  30. \n\
  31.   -B <baud>       Set serial baud rate: 110, 300, 600, 1200, 2400,\n\
  32.                   4800, 9600, or 19200 are valid for <baud>.  Default\n\
  33.                   value is 9600 baud\n\
  34. \n\
  35.   -I <interrupt>  Indicate interrupt used by TFPCX.  Default is 0xFD\n\
  36. \n\
  37.   -U              Unload currently running TFLINK\n\
  38. \n"
  39.  
  40. /* baud rates table */
  41. #define    NUM_BAUD_RATES    8
  42. char    baud_table[NUM_BAUD_RATES][6] = {
  43.     "110", "300", "600", "1200", "2400", "4800", "9600", "19200"
  44. };
  45.  
  46. /* defaults for the various selectable items in this module */
  47. #define TFPCX_DEFAULT_INT    0xFD    /* default interrupt vector */
  48. #define MIN_SIO_PORT        1    /* COM1 lowest port */
  49. #define MAX_SIO_PORT        4    /* COM4 highest port */
  50. #define SIO_DEFAULT_BAUD    9600    /* settle on 9600 baud initially */
  51.  
  52.  
  53. /*-----------------------------------------------------------------------*/
  54. /*
  55.  * Get command line options and store away the information
  56.  * held in them.  Return FALSE if command line error
  57.  */
  58. bool getopts( int argc, char **argv )
  59. {
  60.     int    argp;        /* argument array pointer */
  61.     int    cindex;        /* char index for string traversal */
  62.     char    *optarg;    /* pointer to option argument */
  63.     int    baud_index;    /* baud rate search index */
  64.     bool    retcode;    /* return code for the routine */
  65.     bool    pflag, bflag, iflag;    /* local flags */
  66.  
  67.     /* initialise defaults on all globals we control */
  68.     pflag = bflag = iflag = FALSE;
  69.     nflag = rflag = uflag = FALSE;
  70.     tfpcx_interrupt    = TFPCX_DEFAULT_INT;
  71.     sio_baud    = SIO_DEFAULT_BAUD;
  72.  
  73.     /* start assuming everything is OK */
  74.     retcode = TRUE;
  75.  
  76.     /* loop over all arguments except the command name in [0] */
  77.     for ( argp = 1; argp < argc; argp++ ) {
  78.         if (argv[argp][0] == '-' && toupper(argv[argp][1]) == 'H'){
  79.             printf( HELPMSG );
  80.             exit( 0 );
  81.         }
  82.         else if (argv[argp][0] == '-' && toupper(argv[argp][1]) == 'N'){
  83.             nflag = TRUE;
  84.         }
  85.         else if (argv[argp][0] == '-' && toupper(argv[argp][1]) == 'R'){
  86.             rflag = TRUE;
  87.         }
  88.         else if (argv[argp][0] == '-' && toupper(argv[argp][1]) == 'U'){
  89.             uflag = TRUE;
  90.         }
  91.         else if (argv[argp][0] == '-' && toupper(argv[argp][1]) == 'B'){
  92.             bflag = TRUE;
  93.             optarg = ( strlen( argv[argp] ) == 2 ) ?
  94.                     argv[++argp] : argv[argp] + 2;
  95.             if ( argp == argc ) { retcode = FALSE; break; }
  96.             for ( baud_index = 0; baud_index < NUM_BAUD_RATES;
  97.                         baud_index++ ) {
  98.                 if ( strcmp(optarg, baud_table[baud_index]) == 0 ) {
  99.                     sscanf( optarg, "%d", &sio_baud );
  100.                     break;
  101.                 }
  102.             }
  103.             if ( baud_index >= NUM_BAUD_RATES ) {
  104.                 fprintf( stderr,
  105.                     "Unrecognised baud rate %s\n", optarg );
  106.                 retcode = FALSE; break;
  107.             }
  108.         }
  109.         else if (argv[argp][0] == '-' && toupper(argv[argp][1]) == 'I'){
  110.             iflag = TRUE;
  111.             optarg = ( strlen( argv[argp] ) == 2 ) ?
  112.                     argv[++argp] : argv[argp] + 2;
  113.             if ( argp == argc ) { retcode = FALSE; break; }
  114.             for ( cindex=0; cindex<strlen( optarg ); cindex++ ) {
  115.                 optarg[cindex] = toupper( optarg[cindex] );
  116.             }
  117.             if ( *optarg == '0' && *(optarg+1) == 'X' )
  118.                 optarg += 2;
  119.             if ( sscanf( optarg, "%02x", &tfpcx_interrupt ) != 1 ||
  120.                     strlen( optarg ) != 2 ) {
  121.                 fprintf( stderr,
  122.                     "Bad interrupt value %s\n", optarg );
  123.                 retcode = FALSE; break;
  124.             }
  125.         }
  126.         else if (argv[argp][0] == '-' && toupper(argv[argp][1]) == 'P'){
  127.             pflag = TRUE;
  128.             optarg = ( strlen( argv[argp] ) == 2 ) ?
  129.                     argv[++argp] : argv[argp] + 2;
  130.             if ( argp == argc ) { retcode = FALSE; break; }
  131.             for ( cindex=0; cindex<strlen( optarg ); cindex++ ) {
  132.                 optarg[cindex] = toupper( optarg[cindex] );
  133.             }
  134.             if ( sscanf( optarg, "COM%1d", &sio_port ) != 1 ||
  135.                     strlen( optarg ) != strlen( "COMx" ) ||
  136.                     sio_port < MIN_SIO_PORT ||
  137.                     sio_port > MAX_SIO_PORT ) {
  138.                 fprintf( stderr,
  139.                     "Unrecognised port %s\n", optarg );
  140.                 retcode = FALSE; break;
  141.             }
  142.         }
  143.         else {
  144.             /* some unrecognised option */
  145.             retcode = FALSE; break;
  146.         }
  147.     }
  148.  
  149.     /* check for odd flag combinations and mandatory -P */
  150.     if ( ! pflag )
  151.         retcode = FALSE;
  152.     if ( uflag && ( bflag || iflag ))
  153.         retcode = FALSE;
  154.  
  155.     /* print usage if needed and return */
  156.     if (! retcode) {
  157.         fprintf( stderr, USAGE );
  158.     }
  159.     return ( retcode );
  160. }
  161.